<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BAttribute
 * this class use to save attribute for object, these objects can be 
 * space, app, operation or other obj
 *
 * @author Jianfeng
 */
class BAttribute extends BTreeEntity {

    const Value_Type_String = 'string';
    const Value_Type_None_String = 'none_string';

    //put your code here
    public $id;
    public $parentId;
    public $ownerId;
    public $ownerType;
    public $name;
    public $displayName;
    public $resourceClass;
    public $value;
    public $description;
    public $data = array();
    public $valueType;

    public function getValue($data = NULL) {
        if ($this->valueType == self::Value_Type_None_String) {
            if ($this->data != NULL) {
                $this->value = unserialize($this->data);
            }
        }
        return $this->value;
    }

    public function getName($data = NULL) {
        return $this->name;
    }

    public function getPropertyToColumnMapList($data = NULL) {
        $list = parent::getPropertyToColumnMapList($data);
        $list['parentId'] = 'parent_id';
        $list['ownerType'] = 'resource_class';
        $list['ownerId'] = 'resource_id';
        $list['valueType'] = 'value_type';
        $list['data'] = 'data';
        return $list;
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $repository->addPropertyToColumnMap('ownerId', 'resource_id');
        $repository->addPropertyToColumnMap('ownerType', 'resource_class');
        $repository->addPropertyToColumnMap('valueType', 'value_type');
        $repository->addPropertyToColumnMap('data', 'data');
        $repository->addSearchablePropertyList('ownerId');
        $repository->addSearchablePropertyList('ownerType');
        $repository->addSearchablePropertyList('name');
        $repository->addSearchablePropertyList('parentId');
        return $repository;
    }

    public function addChildItem($item, $data = null) {
        $itemContext = NULL;
        if ($this->ownerType != NULL && $this->ownerId != NULL) {
            if ($item instanceof BAttribute) {
                $item->ownerType = $this->ownerType;
                $item->ownerId = $this->ownerId;
            }
            if (is_array($item)) {
                $item['ownerType'] = $this->ownerType;
                $item['ownerId'] = $this->ownerId;
                $itemContext = array();
                $itemContext['name'] = NULL;
                $itemContext['value'] = NULL;
            }
            if (is_string($item)) {
                $class = get_class($this);
                $name = $item;
                $item = new $class();
                $item->name = $name;
                $item->ownerType = $this->ownerType;
                $item->ownerId = $this->ownerId;
            }
        }

        if ($item instanceof BAttribute) {
            $class = get_class($item);
            $itemContext = new $class();
            $itemContext->ownerType = $item->ownerType;
            $itemContext->ownerId = $item->ownerId;
        }
        if ($data == NULL)
            $data = $itemContext;

        return parent::addChildItem($item, $data);
    }

    public function getChild($context, $recursive = FALSE) {
        if (is_string($context)) {
            $name = $context;
            $context = array();
            $context['name'] = $name;
            $context['ownerType'] = $this->ownerType;
            $context['ownerId'] = $this->ownerId;
        }
        if (is_array($context)) {
            if (!isset($context['ownerType']))
                $context['ownerType'] = $this->ownerType;
            if (!isset($context['ownerId']) && ($this->ownerId != 0 || $this->ownerId != NULL))
                $context['ownerId'] = $this->ownerId;
        }
        if ($context instanceof BAttribute) {
            if (($this->ownerId != NULL || $this->ownerId != 0) && $context->ownerId == NULL)
                $context->ownerId = $this->ownerId;
            if ($this->ownerType != NULL && $context->ownerType == NULL)
                $context->ownerType = $this->ownerType;
        }
        return parent::getChild($context, $recursive);
    }

    public function save($context = null) {
        return parent::save($context);
    }

    public static function getEntityAttribute($entity, $name) {
        $className = __CLASS__;
        $data = array();
        $data['attributeClass'] = $className;
        if ($entity instanceof BEntity) {
            return $entity->getAttribute($name, $data);
        }
        return FALSE;
    }

    public static function addEntityAttribute($entity, $name, $value, $data = array()) {

        $className = __CLASS__;
        $data = array();
        $data['attributeClass'] = $className;
        if ($entity instanceof BEntity) {
            return $entity->addAttribute($name, $value, $data);
        }
        return FALSE;
    }

    public static function removeEntityAttribute($entity, $name, $data = array()) {
        $className = __CLASS__;
        $data = array();
        $data['attributeClass'] = $className;
        if ($entity instanceof BEntity) {
            return $entity->removeAttribute($name, $data);
        }
        return FALSE;
    }

}

?>
